package boc.sx.gateway.core;

import boc.sx.gateway.core.strategy.LBWay;
import boc.sx.gateway.core.strategy.LoadBalanceStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;

/*
 * @author zmx
 * @date 2022/8/9 11:19
 */
@Component
@Slf4j
public class TransferHandler {

    @Resource
    public HashMap<LBWay, LoadBalanceStrategy> lbStrategy;

    @Resource
    HashMap<String,ServiceNode> serverURLInfo;

    public String voteOne(ServiceNode serviceNode){
        if(serviceNode ==null){
            log.error("没有找到对应的服务地址，检查服务代码是否正确");
            return null;
        }
        LBWay lbWay = serviceNode.lbWay;
        //根据id 找到指定额度策略
        LoadBalanceStrategy loadBalanceStrategy = lbStrategy.get(lbWay) == null ? lbStrategy.get(LBWay.POLLING) :lbStrategy.get(lbWay);
        String vote = loadBalanceStrategy.vote(serviceNode);
        return vote;
    }

    public String voteOne(String code){
        ServiceNode serviceNode = serverURLInfo.get(code);
        return voteOne(serviceNode);
    }

}
